<!--
 * @Author: 45F
 * @LastEditors: carol
 * @Description: 布局
 * @Date: 2019-07-06 13:41:42
 * @LastEditTime: 2019-10-26 18:07:09
 -->
<template>
  <a-layout id="components-layout-demo-fixed" style="height: 100%;">
    <a-layout-header
      :style="{
        position: 'fixed',
        zIndex: 1,
        width: '100%',
        background: '#184C97',
        padding: '0 50px 0 10px'
      }"
    >
      <div class="user-ctr">
        <!-- <img src="" class="cover"> -->
        <!-- <a-icon
          class="big-show-icon"
          type="rollback"
          @click="$router.push('/homePage')"
          title="返回首页"
        /> -->
        <!-- <a-icon class="big-show-icon" type="desktop" @click="$router.push('/rkdt')" title="大屏展示人口动态" /> -->
        <a href="javascript:;" @click="logout">退出</a>
        <!-- <a-dropdown class="user">
          <a class="ant-dropdown-link" href="javascript:;">
            {{ user.fullName }}
            <a-icon type="down" />
          </a>
          <a href="javascript:;" @click="logout">退出</a>
          <a-menu slot="overlay">
            <a-menu-item>
              <a href="javascript:;" @click="logout">退出</a>
              <a href="javascript:;" @click="updatePassword">修改密码</a>
            </a-menu-item>
          </a-menu>
        </a-dropdown> -->
      </div>
      <div class="logo">
        <img :src="logo" />
        <span>数据交换平台</span>
      </div>
    </a-layout-header>
    <!-- <a-layout-sider id="home-side-container" width="200" style="background: transparent" v-show="show"> -->
    <a-layout-sider
      id="home-side-container"
      v-show="show"
      :trigger="null"
      collapsible
      v-model="collapsed"
      :style="
        collapsed
          ? {
              flex: '0 0 39px',
              'max-width': '39px',
              'min-width': '39px',
              width: '39px',
              background: 'transparent'
            }
          : {
              flex: '0 0 200px',
              'max-width': '200px',
              'min-width': '200px',
              width: '200px',
              background: 'transparent'
            }
      "
    >
      <a-icon
        :title="collapsed ? '展开菜单' : '收缩菜单'"
        style="color: #184c97"
        class="trigger"
        :type="collapsed ? 'menu-unfold' : 'menu-fold'"
        @click="collapsedSider"
      />
      <a-menu
        v-model="active"
        id="page-menu"
        theme="light"
        mode="inline"
        :inlineCollapsed="collapsed"
        :style="{ lineHeight: '30px', paddingBottom: '100px' }"
        @click="pushRouter"
      >
        <template v-for="(item, index) in menuList">
          <a-menu-item
            :key="item.path || index"
            v-if="item.userType === 1"
            style="padding-left: 10px !important; padding-right: 10px !important;margin: 0px"
          >
            <img class="menu-icon" :src="item.icon" :title="item.name" />
            <span :title="item.name">{{ item.name }}</span>
          </a-menu-item>
        </template>
        <!-- 由横向菜单栏变成侧边菜单栏，这里的moreList可以去掉 -->
        <!-- <a-sub-menu v-if="moreList.length">
          <span slot="title" class="submenu-title-wrapper">
            <a-icon type="appstore" />
          </span>
          <template v-for="(item, index) in moreList">
            <a-menu-item :key="item.path || index">{{item.name}}</a-menu-item>
          </template>
        </a-sub-menu> -->
      </a-menu>
    </a-layout-sider>
    <a-layout-content
      id="home-content-container"
      :style="{ marginTop: '64px' }"
    >
      <router-view />
    </a-layout-content>
    <!-- mqtt通信组件 -->
    <!-- <Mqtt ref="mqtt" @messageArrived="onMessageArrived"></Mqtt> -->
  </a-layout>
</template>

<script>
// import Mqtt from './components/mqtt'
import { auth } from "@/api";
import config from "@/config";
import {
  getUser,
  removeUser,
  setToken,
  removeToken,
  getRoutes,
  setRoutes,
  removeRoutes
} from "@/libs/util";
import logo from "@/assets/images/logo.png";
import { mapState } from "vuex";
import { setUser } from "@/libs/util";
export default {
  // components: { Mqtt },
  data() {
    return {
      collapsed: false,
      menuCollapsed: true,
      logo,
      user: {},
      routes: [],
      menuList: [],
      moreList: [],
      active: [],
      num: config.menuNum,
      reconnectTimeout: 2000,
      show: true,
      // waitNumMap: {
      //   incidentNum: 0,
      //   otherNum: 0
      // },
      list: [
        "asj_task_tdrqjrgjqy",
        "asj_task_cdrmanru",
        "asj_task_tdryjtgj",
        "asj_task_snzxwz",
        "asj_task_tdryjjgj",
        "asj_task_cdrlikai",
        "asj_task_shineilishiguiji",
        "xnsf"
      ]
    };
  },
  computed: {},
  watch: {
    $route(data) {
      this.setActive(data);
      let name = this.$router.currentRoute.name;
      this.show = !this.list.includes(name);
    },
    routes(data) {
      this.handleMenu(data);
      let name = this.$router.currentRoute.name;
      this.show = !this.list.includes(name);
    }
  },
  mounted() {
    // this.$refs.mqtt.buildConnect() // 建立mqtt通信
    this.user = getUser();
    this.setActive(this.$route);
    this.getMenu();
    setInterval(() => {
      this.refreshToken();
    }, this.$config.refreshTimeToken);
    // 计算侧边栏的高度
    // let winHeight = window.innerHeight
    // document.getElementById('home-side-container').style.height = (winHeight - 64) + 'px'
    // document.getElementById('home-content-container').style.height = (winHeight - 64) + 'px'
    // ant-layout-content
  },
  beforeDestroy() {
    // this.$refs.mqtt.disconnect() // 关闭页面断开mqtt连接
  },
  methods: {
    // 接收mqtt消息
    onMessageArrived(msg) {
      console.log("index页面 mqtt消息 ：");
      console.log(msg);
      if (msg) {
        // this.openNotification(msg)
      }
    },
    collapsedSider() {
      this.collapsed = !this.collapsed;
      // this.menuCollapsed = this.collapsed
      document.getElementsByClassName(
        "ant-layout-sider-children"
      )[0].style.width = this.collapsed ? "40px" : "200px";
    },
    openNotification(msg) {
      const key = `open${Date.now()}`;
      this.$notification.config({
        top: "62px",
        duration: 10
      });
      this.$notification.open({
        icon: () => {
          return <a-icon type="exclamation-circle" style="color:#FAAE18" />;
        },
        message: msg.title,
        description: msg.content,
        btn: h => {
          return h(
            "a-button",
            {
              props: {
                type: "primary",
                size: "small"
              },
              on: {
                click: () => this.$notification.close(key)
              }
            },
            "处置"
          );
        },
        key
      });
    },
    disposeClick(param) {},

    pushRouter(option) {
      // debugger
      this.$router.push({
        path: option.key // 点击菜单，切换路由，option.key就是路由的path
      });
      this.active = [option.key];
    },
    setActive(route) {
      //注意，菜单文字变蓝色，要加个子节点(span)
      this.active = [route.meta.activeInMenu]; // 当前菜单选中状态(不是点击选中，是回显选中)，active（v-model）为对应的key,即为activeInMenu(path)
    },
    logout() {
      removeToken();
      removeUser();
      removeRoutes();
      this.$router.push({
        name: "login"
      });
    },
    updatePassword() {
      this.$router.push({
        name: "updatePassword"
      });
    },
    getMenu() {
      let userType = this.user.userType;
      // debugger
      this.routes = [
        {
          name: "首页",
          menuType: 0,
          icon: "zdqygl.png",
          path: "/data_exchange/home",
          identification: "",
          routes: [],
          userType: userType
        },
        {
          name: "资产明细",
          menuType: 0,
          icon: "model.png",
          path: "/data_exchange/zcmx",
          identification: "",
          routes: [],
          userType: 1
        },
        {
          name: "资源请求",
          menuType: 0,
          icon: "dpjcqypz.png",
          path: "/data_exchange/zyqq",
          routes: [],
          userType: userType
        },
        {
          name: "消费记录",
          menuType: 0,
          icon: "qbwj.png",
          path: "/data_exchange/xfjl",
          identification: "",
          routes: [],
          userType: userType
        },
        {
          name: "用户管理",
          menuType: 0,
          icon: "rkdtgk.png",
          path: "/data_exchange/yhgl",
          identification: "",
          routes: [],
          userType: userType
        },
        {
          name: "角色管理",
          menuType: 0,
          icon: "process.png",
          path: "/data_exchange/jsgl",
          identification: "",
          routes: [],
          userType: userType
        }
      ];
    },
    refreshToken() {
      auth.getRefreshToken().then(res => {
        setToken(res.data.data);
      });
    },
    handleMenu(data) {
      data.forEach((item, index) => {
        if (item.icon) {
          try {
            item.icon = require(`./images/${item.icon}`);
          } catch (e) {
            item.icon = require(`./images/event.png`);
          }
        }
        this.menuList.push(item);
      });
    }
  }
};
</script>
<style>
li.ant-menu-item.ant-menu-item-selected span {
  color: blue;
}
.ant-menu-item.ant-menu-item-active {
  background-color: #e6f7ff;
}
.ant-layout-sider-children {
  width: 200px;
}
#components-layout-demo-fixed .trigger {
  font-size: 18px;
  padding: 0 12px;
  cursor: pointer;
  width: 40px;
  height: 40px;
  line-height: 40px;
}

#components-layout-demo-fixed .trigger:hover {
  color: #1890ff;
}
/* #components-layout-demo-fixed .ant-menu.ant-menu-inline.ant-menu-root.ant-menu-light{
    width: 200px;
  } */
.my-title {
  min-height: 60px;
}
#components-layout-demo-fixed .ant-menu-inline-collapsed {
  width: 40px;
  overflow: hidden;
  text-align: left;
}
.ant-layout-sider.ant-layout-sider-dark ant-layout-sider-collapsed {
  flex: 0 0 40px !important;
}
#page-menu {
  border: none;
}
#page-menu > .ant-menu-submenu > .ant-menu-submenu-title,
#page-menu > .ant-menu-item > .first-title {
  padding: 0 10px !important;
}
</style>
<style lang="less">
.ant-layout-sider-children {
  // width: 200px;
  position: fixed;
  z-index: 999;
  overflow: auto;
  background: #ffffff;
  transition: all 0.3s ease 0s;
}
#home-side-container .ant-menu-submenu-selected {
  .ant-menu-submenu-title {
    font-weight: bold;
  }
}
#home-side-container .ant-menu-submenu-inline {
  .ant-menu-submenu-title {
    font-family: Microsoft YaHei;
    height: 60px;
    line-height: 60px;
  }
}
::-webkit-scrollbar-thumb {
  background: #aeb1b5;
  height: 20px !important;
}
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
  background: #ffffff;
}
#home-side-container,
home-content-container {
  overflow: auto;
}
</style>
<style scoped lang="less">
.big-show-icon {
  margin-right: 15px;
  font-size: 16px;
  color: #ffffff;
  cursor: pointer;
}
.menu-icon {
  height: 20px;
  width: 20px;
  margin-right: 10px;
}
#home-side-container {
  margin-top: 64px;
  z-index: 1000;
  position: relative;
  /* height: 100%; */
  .ant-menu-item {
    height: 60px;
    color: #4d4d4d;
    line-height: 60px;
  }
}
.ant-menu-submenu-title {
  height: 60px !important;
  color: #4d4d4d;
  line-height: 60px !important;
}
.logo {
  margin-right: 60px;
  float: left;
  font-size: 16px;
  color: #fff;
  img {
    width: 36px;
    height: 31px;
  }
  span {
    margin-top: 3px;
    margin-left: 12px;
  }
}
.ant-menu {
  font-size: 16px;
}
.ant-layout-content {
  padding: 10px;
}
.wait-num-box {
  display: inline-block;
  width: 17px;
  height: 17px;
  background: rgba(255, 0, 0, 1);
  border-radius: 50%;
  color: #fff;
  text-align: center;
  line-height: 17px;
  font-size: 12px;
  margin-left: 5px;
}
/* 滚动条样式 */
// .wsEcharts-container::-webkit-scrollbar-thumb{
//   background: rgb(12,14,39);
//   height: 20px !important
// }
// .wsEcharts-container::-webkit-scrollbar {
//   width: 4px;
//   background: rgba(26, 41, 61, 0.23)
// }
// .ant-layout-content::-webkit-scrollbar-thumb{
//   background: rgb(123,137,159) !important;
//   height: 20px !important
// }
// .ant-layout-content::-webkit-scrollbar {
//     width: 4px !important;
//     height: 4px !important;
//     background: rgb(21,44,69) !important
//   }
</style>
